<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model="params.currencyName" placeholder="货币名称" style="width: 200px;" class="filter-item" clearable @keyup.enter.native="fetchData" />
      <el-input v-model="params.currencyCode" placeholder="货币代码" style="width: 200px;margin-left: 10px;" class="filter-item" clearable @keyup.enter.native="fetchData" />
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" style="margin-left: 10px;" @click="fetchData">
        搜索
      </el-button>
      <el-button type="success" icon="el-icon-warning-outline" class="filter-item" @click="resetForm()">重置</el-button>

    </div>
    <div class="filter-container">
      <el-button class="filter-item" type="primary" icon="el-icon-edit" @click="editOrAdd(0,0)">
        添加
      </el-button>
    </div>
    <el-table
      v-loading="listLoading"
      :data="list"

      element-loading-text="Loading"
      border
      fit
      highlight-current-row
    >
      <el-table-column label="币种名称">
        <template slot-scope="scope">
          {{ scope.row.currencyName }}
        </template>
      </el-table-column>
      <el-table-column label="币种代码" align="center">
        <template slot-scope="scope">
          {{ scope.row.currencyCode }}
        </template>
      </el-table-column>
      <el-table-column label="汇率" align="center">
        <template slot-scope="scope">
          {{ scope.row.exchangeRate }}
        </template>
      </el-table-column>
      <el-table-column align="center" prop="created_at" label="操作">
        <template slot-scope="scope">
          <el-row>
            <el-button type="text" @click="editOrAdd(1,scope.row)">编辑</el-button>
            <el-button  type="text" style="color:red" @click="deleteUser(scope.row.id)">删除</el-button>
          </el-row>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="params.pageNum" :limit.sync="params.pageSize" @pagination="fetchData" />

    <el-dialog :title="dialogTitles[dialogIndex]" :visible.sync="dialogFormVisible">
      <el-form ref="dataForm" :rules="rules" :model="adminData" label-width="80px" style="margin-left:100px;margin-right: 100px">
        <el-form-item label="货币名称" prop="currencyName">
          <el-input v-model="adminData.currencyName"  />
        </el-form-item>

        <el-form-item label="货币代码" prop="currencyCode">
          <el-input v-model="adminData.currencyCode" />
        </el-form-item>
        <el-form-item label="汇率" prop="货币代码">
          <el-input v-model="adminData.exchangeRate" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel()">
          取消
        </el-button>
        <el-button type="primary" @click="save">
          确定
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getList, roleList, deleteUser, addUser, updateStatus } from '@/api/admin'
import { currencyList,addCurrency,deleteCurrency} from '@/api/huobi'
import Pagination from '@/components/Pagination'
import waves from '@/directive/waves'
import Calendar from '@/components/Cascader'
import { nowPassword } from '../../utils/constant'
export default {
  components: { Pagination, Calendar },
  directives: { waves },
  data() {
    return {
      list: [],
      listLoading: true,
      total: 10,
      dialogTitles: ['添加', '编辑'],
      dialogIndex: 0,
      dialogFormVisible: false,
      adminData: {},
      params: {
        pageNum: 1,
        pageSize: 10,
        currencyName: '',
        currencyCode: ''
      },
      rules: {
           currencyName: [{ required: true, message: '请输入货币名称', trigger: 'blur' }],
            currencyCode: [{ required: true, message: '请输入货币代码', trigger: 'blur' }],
            exchangeRate: [{ required: true, message: '请输入货币汇率', trigger: 'blur' }],
      },
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      this.listLoading = true
      currencyList(this.params).then(response => {
        this.total = response.total
        this.list = response.rows
        this.listLoading = false
      })
    },
    cancel() {
      this.dialogFormVisible = false
    },
    editOrAdd(index, item) {
      this.adminData = Object.assign({}, item)
      if (index === 1) {
        delete this.adminData.createTime
      }
      this.dialogIndex = index
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    save() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          addCurrency(this.adminData).then(res => {
            this.dialogFormVisible = false
            this.$message({
              type: 'success',
              message: this.dialogTitles[this.dialogIndex] + '成功!'
            })
            this.fetchData()
          })
        }
      })
    },
    updateStatus(id) {
      updateStatus(id).then(res => {
        this.$message({
          type: 'success',
          message: '成功!'
        })
        this.fetchData()
      })
    },
    deleteUser(id) {
      this.$confirm('此操作将永久删除该币种, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteCurrency({id:id}).then(res => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.fetchData()
        })
      })
    },
    resetForm() {
      this.params = {
        pageNum: 1,
        pageSize: 10
      }
      this.$nextTick(() => {
        this.fetchData()
      })
    }
  }
}
</script>
<style lang="scss" scoped>
    /deep/.el-dialog{
            width:40%
    }
</style>
